﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Freecart_Local_.Classes
{
    public class WebPageBaseClass : System.Web.UI.Page
    {
        private static bool debugmode = true;

        public WebPageBaseClass()
        {
            // constructor
        }

        protected override void OnLoad(EventArgs e)
        {
            //
            // This is where the magic takes place...
            //

            base.OnLoad(e);

            Cart cart;
            Guid guid;
            
            if (!Page.IsPostBack)
            {
                 // not postback
                 if (Page.Session["guid"] == null)
                 {
                     // user no longer exists, recreate guid and cart
                     guid = System.Guid.NewGuid();
                     Page.Session["guid"] = guid;
                     
                     // create cart new instance
                     cart = new Cart();
                     cart.Guid = guid;

                     // store cart in cart manager
                     CartManager.AddCart(cart);
                 }
                 else
                 {
                     // session guid found, declare guid again
                     guid = (Guid)Page.Session["guid"];

                     // restore cart instance
                     cart = CartManager.FetchCartByGuid(guid);
                 }


                 if (debugmode == true)
                 {
                     this.Page.Title = "Freecart Demo (Guid: " + guid + ")";

                     cart.Items.Add(new CartRow(1, 4, "Butterflies", 1.32));
                     cart.Items.Add(new CartRow(2, 8, "Ant Farm", 2.32));
                     cart.Items.Add(new CartRow(3, 2, "Chocolate Crickets", 3.32));
                 }
             }
             else
             {
                 // postback
             }
        }
    }
}
